//
//  ViewController.m
//  UI15_UICollectionViewCycleLife
//
//  Created by dllo on 16/8/3.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "ViewController.h"
#define kCollectionView @"reuse"
#import "NyCollectionViewCell.h"


@interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createCollectionView];
    
}
- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((self.view.bounds.size.width - 30) / 2, 100);
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 5;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[NyCollectionViewCell class] forCellWithReuseIdentifier:kCollectionView];
    
    // 1. numberOfSectionsInCollectionView
    // 2. numberOfItemsInSection
    // 注意 3 4 循环执行
    // 3. prepareForReuse (从重用池区cell时调用)
    // 4. cellForItemAtIndexPath
    // 5. willDisplayCell
    // 6. didEndDisplayingCell (当cell完全出屏幕时)
    // iOS 10 之前出屏幕立刻进入重用池 3 4 5
    // iOS之后 出屏幕立即返回 cell不会进入重用池
    
    
    
}
// 区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    NSLog(@"111111111111[%@ : %s]", self, __FUNCTION__);
    return 1;
}
// 每个区 对应的cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSLog(@"111111111111[%@ : %s]", self, __FUNCTION__);
    return 700;
}
// 从重用池取出cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionView forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0f green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:1];
    return cell;
}

// cell将要显示在屏幕时
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"111111111111[%@ : %s]", self, __FUNCTION__);
}
// cell已经离开屏幕时
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@ ------------------ %s", self, __FUNCTION__);
}













- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
